﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Upreader.Usenet.Nntp
{
    /// <summary>
    /// Overrides Close and Dispose for only flushing not disposing the internal BaseStream
    /// </summary>
    sealed class NonClosableStream : Stream
    {
        #region Fields

        private readonly Stream _baseStream;

        private bool _closed;

        #endregion // Fields

        #region Constructor

        public NonClosableStream(Stream baseStream)
        {
            if (baseStream == null)
                throw new ArgumentNullException("baseStream");

            _baseStream = baseStream;
        }

        #endregion // Constructor

        #region Properties

        public Stream BaseStream { get { return _baseStream; } }

        public override bool CanRead
        {
            get { ValidateObjectState(); return BaseStream.CanRead; }
        }

        public override bool CanSeek
        {
            get { ValidateObjectState(); return BaseStream.CanSeek; }
        }

        public override bool CanWrite
        {
            get { ValidateObjectState(); return BaseStream.CanWrite; }
        }

        public override long Length
        {
            get { ValidateObjectState(); return BaseStream.Length; }
        }

        public override long Position
        {
            get
            {
                ValidateObjectState(); return BaseStream.Position;
            }
            set
            {
                ValidateObjectState(); BaseStream.Position = value;
            }
        }

        #endregion // Properties

        #region Methods

        public override void Flush()
        {
            ValidateObjectState();
            BaseStream.Flush();
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            ValidateObjectState();
            return BaseStream.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            ValidateObjectState();
            return BaseStream.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            ValidateObjectState();
            BaseStream.SetLength(value);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            ValidateObjectState();
            BaseStream.Write(buffer, offset, count);
        }

        public override void Close()
        {
            if (!_closed)
            {
                BaseStream.Flush();
            }

            _closed = true;
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        #endregion // Methods

        #region Helpers

        private void ValidateObjectState()
        {
            if (_closed)
                throw new InvalidOperationException(Resource.NonClosableStream_Closed);
        }

        #endregion // Helpers
    }
}
